package com.wn55.hrms.web.controller;

import com.github.pagehelper.PageInfo;
import com.wn55.hrms.entity.Position;
import com.wn55.hrms.entity.PositionLevel;
import com.wn55.hrms.service.PositionService;
import com.wn55.hrms.service.vo.PositionVo;
import com.wn55.hrms.util.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 赵明杰
 * @date 2022/05/05/18:25
 */
@RestController
@RequestMapping("position")
public class PositionController {
    @Autowired
    PositionService positionService;

    @GetMapping("/initPosition")
    @Cacheable(value = "positionData",keyGenerator = "keyGenerator")
    public ResultResponse<List<Position>> initPosition(){
     ResultResponse<List<Position>>result = null;
     try{
         List<Position> positions = positionService.initPosition();
         result = new ResultResponse<>(200,"加载成功",positions);
     }catch (Exception e){
         e.printStackTrace();
         result = new ResultResponse<>(201,"加载失败");
     }
       return result;
    }

    @GetMapping("/listAllPosition")
    @Cacheable(value = "positionPagesData",keyGenerator = "keyGenerator")

    public ResultResponse<PageInfo<PositionVo>> listAllPosition(@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
                                                                @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                                                String name){
        ResultResponse<PageInfo<PositionVo>> result = null;
        try {
            PageInfo<PositionVo> pageInfo = positionService.selectPositionByCondition(currentPage,pageSize,name);
            result =new ResultResponse<>(200,"查询职位成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询职位失败");
        }
        return result;

    }

    /**
     * 添加职位
     * @param position
     * @return
     */
    @PostMapping("/addPosition")
    public ResultResponse<Void> addPosition(@RequestBody Position position){
       ResultResponse<Void> result = null;
        try {
            positionService.addPosition(position);
            result = new ResultResponse<>(200,"添加职位成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加职位失败");
        }
        return result;
    }

    /**
     * 修改职位
     * @param position
     * @return
     */
    @PutMapping("/editPosition")
    public ResultResponse<Void> editPosition(@RequestBody Position position){
        ResultResponse<Void> result = null;
        try {
            positionService.editPosition(position);
            result = new ResultResponse<>(200,"修改职位成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"更新部门失败");
        }
        return result;
    }

    /**
     * 删除职位
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResultResponse<Void> delete(@PathVariable("id") int id){
        ResultResponse<Void> result = null;
        try {
            positionService.deletePositionById(id);
            result = new ResultResponse<>(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除部门失败");
        }
        return result;

    }
    @GetMapping("/initPositionByDept/{deptId}")
    public ResultResponse<List<Position>> initPositionByDept(@PathVariable("deptId") Integer deptId){
        ResultResponse<List<Position>>result = null;
        try{
            List<Position> positions = positionService.initPositionByDept(deptId);
            result = new ResultResponse<>(200,"加载成功",positions);
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"加载失败");
        }
        return result;
    }
}
